package com.sgb.kjwl.view.ui.activity.demand;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDemandAddBinding;
import com.sgb.kjwl.model.entity.DemandGoodsEntity;
import com.sgb.kjwl.view.event.self.DemandEvent;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS;

/**
 * Description:
 * Author CFK
 * Time
 */
public class DemandAddActivity extends AppActivity {
    private ActivityDemandAddBinding binding;
    private DemandGoodsEntity demandGoodsEntity;
    private DemandEvent demandEvent;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, DemandAddActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        demandGoodsEntity = new DemandGoodsEntity();
        binding = DataBindingUtil.setContentView(DemandAddActivity.this, R.layout.activity_demand_add);
        binding.setViewModel(demandGoodsEntity);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        binding.include.rightView.setText("保存");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.titleView.setText("添加商品");
        binding.tvNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (binding.tvNumber.getText().toString().substring(0).equals(".")) {
                    binding.tvNumber.setText("");
                }
            }
        });
    }


    @Override
    protected void init() {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                if (TextUtils.isEmpty(binding.tvName.getText())) {
                    MToast.showToast(DemandAddActivity.this, "请填写名称");
                    return;
                } else if (TextUtils.isEmpty(binding.tvSpec.getText())) {
                    MToast.showToast(DemandAddActivity.this, "请填写型号");
                    return;
                } else if (TextUtils.isEmpty(binding.tvBrand.getText())) {
                    MToast.showToast(DemandAddActivity.this, "请填写品牌");
                    return;
                } else if (TextUtils.isEmpty(binding.tvNumber.getText())) {
                    MToast.showToast(DemandAddActivity.this, "请填写数量");
                    return;
                } else if (TextUtils.isEmpty(binding.tvUnit.getText())) {
                    MToast.showToast(DemandAddActivity.this, "请填写单位");
                    return;
                }
                if (demandEvent == null) {
                    demandEvent = new DemandEvent();
                }
                demandEvent.setMessageTag(EVENT_ADD_GOOGS);
                demandEvent.setObject(binding.getViewModel());
                EventBus.getDefault().post(demandEvent);
                finish();
                break;
        }
    }
}
